package com.chagee.store.operation.server.mq;

import com.alibaba.fastjson.JSON;
import com.chagee.store.operation.api.enums.StoreWorkOrderStatusEnum;
import com.chagee.store.operation.common.constant.CommonConstant;
import com.chagee.store.operation.server.entity.WorkOrderDO;
import com.chagee.store.operation.server.entity.WorkOrderReceiverDO;
import com.chagee.store.operation.server.event.service.WorkOrderBaseService;
import com.chagee.store.operation.server.event.service.WorkOrderReceiverBaseService;
import com.chagee.store.operation.server.model.param.WorkOrderExpiredTaskMqBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WorkOrderTaskMqProduct  //类名称
 * @Description: 类描述：消费延迟队列内的创建的工单（状态不是待验收，已完成，已过期的），将状态改为已过期。
 * @Author: Jacky-zhan	//作者
 * @CreateDate: 2024/3/21 13:51	//创建时间
 * @UpdateUser: Jacky-zhan
 * @UpdateDate: 2024/3/21 13:51	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = CommonConstant.ROCKET_MQ_GROUP, topic = CommonConstant.CLOSE_CLEAN_ORDER_DELAY_TOPIC)
public class WorkOrderExpiredTaskMqListener implements RocketMQListener<WorkOrderExpiredTaskMqBody>{

	@Resource
	private WorkOrderBaseService workOrderBaseService;

	@Resource
	private WorkOrderReceiverBaseService workOrderReceiverBaseService;


	@Override
	public void onMessage(WorkOrderExpiredTaskMqBody message) {
		log.info("工单创建mq-消费者 message info:{}", JSON.toJSONString(message));
		if (message != null) {
			//工单id
			Long workOrderId = message.getWorkOrderId();
			WorkOrderDO workOrderDO = workOrderBaseService.selectById(workOrderId);
			if (Objects.nonNull(workOrderDO)) {
				if (Objects.nonNull(workOrderDO.getDocumentStatus()) &&
					!StoreWorkOrderStatusEnum.TO_BE_ACCEPTED.getType().equals(workOrderDO.getDocumentStatus()) &&
					!StoreWorkOrderStatusEnum.COMPLETED.getType().equals(workOrderDO.getDocumentStatus()) &&
					!StoreWorkOrderStatusEnum.EXPIRED.getType().equals(workOrderDO.getDocumentStatus()) &&
					!StoreWorkOrderStatusEnum.IN_APPEAL.getType().equals(workOrderDO.getDocumentStatus())&&
					!StoreWorkOrderStatusEnum.EXPIRED_APPEAL.getType().equals(workOrderDO.getDocumentStatus())) {
					//满足不在（待验收，已完成，已过期，+申诉中 +过期申述中）内，将状态改为已过期。
					workOrderDO.setDocumentStatus(StoreWorkOrderStatusEnum.EXPIRED.getType());
					workOrderDO.setExpiredTime(new Date());
					workOrderBaseService.update(workOrderDO);
					//同时修改工单-接单人对应的状态
					List<WorkOrderReceiverDO> workOrderReceiverDOList = workOrderReceiverBaseService.selectListByOrderId(workOrderId);
					if (CollectionUtils.isNotEmpty(workOrderReceiverDOList)) {
						workOrderReceiverDOList = workOrderReceiverDOList.stream().
							peek(workOrderReceiverDO -> workOrderReceiverDO.setDocumentStatus(StoreWorkOrderStatusEnum.EXPIRED.getType()))
							.collect(Collectors.toList());
					}
					workOrderReceiverBaseService.updateBatchById(workOrderReceiverDOList);
					log.info("工单: {}已消费，状态处理为已过期!", JSON.toJSONString(message.getWorkOrderNumber()));
				}
			}
		}
	}
}
